package controller;

import lombok.SneakyThrows;
import services.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @program: personnel_management_system
 * @description: 登录流程管理
 * @author: Mr.Sun
 * @create: 2020-05-21 22:45
 **/

@WebServlet("/login")
public class LoginController extends HttpServlet {
    @SneakyThrows
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.getRequestDispatcher("/WEB-INF/login.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        boolean result = false;
        UserService us = new UserService();
        try {
            System.out.println("front end code:"+req.getParameter("validCode"));
            System.out.println(req.getSession().getAttribute("code"));
            if(req.getParameter("validCode").equals(req.getSession().getAttribute("code"))){
                result = us.isUser(req.getParameter("uname"),req.getParameter("pwd"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(result ){
            System.out.println("user result:"+result);
            req.getRequestDispatcher("/WEB-INF/dist/index.html").forward(req, resp);
        }else {
            System.out.println("user result:"+result);
            req.getRequestDispatcher("/WEB-INF/login.jsp").forward(req, resp);
        }
    }
}
